
package com.lxr0860.lxr0860production_test.itemactivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.utils.RefreshViewUtils;
import com.sdk.devicemanager.Barcode;
import com.sdk.devicemanager.BarcodeConfig;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ScanActivity extends Activity{
    private Button mBtnScan;
    private TextView mTvScanTip;
    private TextView mTvScanResult;

    private Barcode mBarcode;
    private String scanResult;
    private BarcodeConfig mBarcodeConfig;
    private static final int SCAN_TEST_COUNT = 30000;
    private int currentCount,openSucC,openFailC,closeSucC,closeFailC,scanSucC,scanFailC;
    private boolean destroy;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Contants.DIALOG_POSITIVE:
                    Intent mIntent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putBoolean("result",true);
                    mIntent.putExtras(bundle);
                    ScanActivity.this.setResult(Contants.TEST_ITEM_SCAN,mIntent);
                    finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    finish();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_barcode);

        initView();
        initData();
        initEvent();
        Log.e("activity","onCreate");
    }

    private void initView() {
        mBtnScan = (Button) findViewById(R.id.btn_scan_start);
        mTvScanTip = (TextView) findViewById(R.id.tv_scan_tip);
        mTvScanResult = (TextView) findViewById(R.id.tv_scan_result);
    }

    private void initData() {
        mBarcode = new Barcode(ScanActivity.this);
        mBarcodeConfig = new BarcodeConfig();
        mBarcodeConfig.mOutPutMode = BarcodeConfig.OUTPUT_API;
        mBarcodeConfig.mEncodeMode = BarcodeConfig.ENCODE_MODE_GBK;
//        mBarcodeConfig.mEncodeMode = BarcodeConfig.ENCODE_MODE_UTF8;
        mBarcodeConfig.mTrigerMode = BarcodeConfig.TRIGGER_MODE_SINGLE;
        mBarcodeConfig.mAutoEnt  = 0;
        mBarcode.setup1D(mBarcodeConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBarcode.cancel1D();
        destroy = true;
    }

    private void initEvent() {
        mBtnScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBtnScan.setEnabled(false);
                currentCount=openSucC=openFailC=closeSucC=closeFailC=scanSucC=scanFailC=0;
                mTvScanTip.setText("");
                mTvScanResult.setText("");
                new ScanThread().start();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy=true;
    }


    private class ScanThread extends Thread {
        @Override
        public void run() {
            if(mBarcode==null){
                return;
            }
            for(int i=0;i<SCAN_TEST_COUNT;i++) {
                boolean open = mBarcode.open1D();

                if (destroy) {
                    mBarcode.close1D();
                    return;
                }
                currentCount++;
                RefreshViewUtils.setText(ScanActivity.this, mTvScanTip,
                        "当前次数(" + currentCount + "/" + SCAN_TEST_COUNT + ")次");

                if (open) {
                    openSucC++;
                    String result = mBarcode.scan1D();
                    Log.e("scanresult",result);
                    scanResult = result;

                    if (!result.equals("")) {
                        Log.e("scan",result);
                        if (containsNewLine(result)) {
                            scanResult = "扫码存在回车换行";
                            scanFailC++;
                        } else {
                            scanSucC++;
                        }
                    } else {
                        scanFailC++;
                    }
                } else {
                    openFailC++;
                }

                boolean close = mBarcode.close1D();
                if (close) {
                    closeSucC++;
                } else {
                    closeFailC++;
                }
                final String msg = "上电成功 = " + openSucC + "次,失败 = " + openFailC + "次,\n\n" +
                        "扫描结果为------->" + scanResult + "\n\n" +
                        "扫描成功 = " + scanSucC + "次,失败 = " + scanFailC + "次,\n" +
                        "下电成功 = " + closeSucC + "次,失败 = " + closeFailC + "次";
                RefreshViewUtils.setText(ScanActivity.this, mTvScanResult, msg);
            }
            if(scanSucC==SCAN_TEST_COUNT){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Contants.showDailog(ScanActivity.this,"测试成功，是否保存",handler, Gravity.TOP);
                    }
                });
            }else {
                RefreshViewUtils.enableView(ScanActivity.this,mBtnScan,true);
            }
        }
    }

    /**
     * 判断是否添存在回车换行
     * @param input
     * @return
     */
    public boolean containsNewLine(String input) {
        Pattern pattern = Pattern.compile("\\r|\\n");
        Matcher matcher = pattern.matcher(input);
        return matcher.find();
    }
    
}